package com.shopowner.service.impl;



import com.shopowner.dao.StoreDao;
import com.shopowner.dto.DTO;
import com.shopowner.dto.MessageEnum;
import com.shopowner.entity.Store;
import com.shopowner.page.PageInfo;
import com.shopowner.service.StoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("storeService")
public class StoreServiceImpl implements StoreService {
    @Resource
    StoreDao storeDao;
    @Override
    public DTO<Store> getAllStores()  throws Exception{
        List<Store> stores = storeDao.getAllStores();
        MessageEnum messageEnum  = Optional.ofNullable(stores).isPresent()?MessageEnum.GETADMINSUCCESS: MessageEnum.GETADMINFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage(),stores);
    }

    @Override
    public DTO<Store> deleteStoreByStno(String stno)  throws Exception{
        Integer result = storeDao.deleteStoreByStno(stno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Store> updateStoreByStno(Store store)  throws Exception{
        Integer result = storeDao.updateStoreByStno(store);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Store> addStore(Store store) throws Exception {
        Integer result = storeDao.addStore(store);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Store> getStoreByPage(PageInfo pageInfo ) throws Exception {
        List<Store> stores = storeDao.getStoreByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(stores).isPresent()?MessageEnum.GETADMINSUCCESS: MessageEnum.GETADMINFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage(),stores,pageInfo);
    }
    @Override
    public DTO<Store> selectStoreWithCoach(String stno)  throws Exception{
        Store store = storeDao.selectStoreWithCoach(stno);
        MessageEnum messageEnum  = Optional.ofNullable(stno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage(),store);
    }
    @Override
    public DTO<Store> selectStoreWithshopowner(String stno) throws Exception{
        Store store = storeDao.selectStoreWithshopowner(stno);
        MessageEnum messageEnum  = Optional.ofNullable(stno).isPresent()?MessageEnum.GETNEWSSUCCESS: MessageEnum.GETNEWSFAIL;
        return new DTO<Store>(messageEnum.getCode(),messageEnum.getMessage(),store);
    }
}
